package com.study.controller;

import com.study.server.WebSocketServer;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;

@Controller
public class CheckCenterController {

    //页面请求
    @GetMapping("/socket/{cid}")
    public ModelAndView socket(@PathVariable String cid) {
        ModelAndView mav=new ModelAndView("center_socket");
        mav.addObject("cid", cid);
        return mav;
    }

    //推送数据接口， cid 可有可无
    @ResponseBody
    @RequestMapping("/socket/push")
    public String pushToWeb(@RequestParam(required = false) String cid, @RequestBody String message) {
        try {
            WebSocketServer.sendInfo(message,cid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;
    }

    //推送数据接口, 必须要指定cid值
    @ResponseBody
    @RequestMapping(value = {"/socket/push1/{cid}"})
    public String pushToWeb1(@PathVariable String cid, @RequestBody String message) {
        try {
            WebSocketServer.sendInfo(message,cid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;
    }

}
